//
//  BrandCustom.m
//  cutter
//
//  Created by mac on 16/1/20.
//  Copyright © 2016年 e360. All rights reserved.
//

#import "AppData+Brand.h"

@implementation AppData (Brand)
+(id)brandClassify{
    if ([[UIDevice currentDevice] network] != UIDeviceNetworkNone) {
        NSArray *items = [self loadJson:@"?n=api&a=brandstory&c=brand_story_class" key:nil data:nil];
        if(items){
            [[[TMSQLite shareInstance] transaction] begin];
            for (id item in items) {
                NSMutableDictionary *temp = [NSMutableDictionary dictionaryWithDictionary:item];
                //
                NSString *sql = [self fromInsert:@"e360_brand_story_class" data:temp];
                [[TMSQLite shareInstance] query:sql];
            }
            [[[TMSQLite shareInstance] transaction] commit];
        }
    }
    return [[TMSQLite shareInstance] query:@"SELECT * FROM e360_brand_story_class WHERE deleted=0"];
}
+(id)brandList:(NSDictionary*)condition{
    if ([[UIDevice currentDevice] network] != UIDeviceNetworkNone) {
        NSString *request = [NSString stringWithFormat:@"?n=api&a=brandstory&c=brand_story&classid=%@&brandid=",[condition objectForKey:@"id"]];
        NSArray *items = [self loadJson:request key:nil data:nil];
        if(items){
            [[[TMSQLite shareInstance] transaction] begin];
            for (id item in items) {
                id albums = [item objectForKey:@"album"];
                for (id album in albums) {
                    NSString *sql = [self fromInsert:@"e360_brand_story_album" data:album];
                    [[TMSQLite shareInstance] query:sql];
                }
                //
                id videos = [item objectForKey:@"video"];
                for (id video in videos) {
                    NSString *sql = [self fromInsert:@"e360_brand_story_video" data:video];
                    [[TMSQLite shareInstance] query:sql];
                }
                //
                NSMutableDictionary *temp = [NSMutableDictionary dictionaryWithDictionary:item];
                NSString *sql = [self fromInsert:@"e360_brand_story" data:temp];
                [[TMSQLite shareInstance] query:sql];
            }
            [[[TMSQLite shareInstance] transaction] commit];
        }
    }
    //
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM e360_brand_story WHERE deleted=0"];
    //
    id user = [TMGlobal valueForKey:@"user"];
    if (user) {
        sql = [sql stringByAppendingFormat:@" AND companyid=%@", [user objectForKey:@"companyid"]];
    }
    //
    NSArray *items = [[TMSQLite shareInstance] query:sql];
    for (id item in items) {
        sql = [NSString stringWithFormat:@"SELECT * FROM e360_brand_story_album WHERE deleted=0 AND brandstoryid=%@",[item objectForKey:@"id"]];
        id album = [[TMSQLite shareInstance] query:sql];
        [item setValue:album forKey:@"album"];
        //
        sql = [NSString stringWithFormat:@"SELECT * FROM e360_brand_story_video WHERE deleted=0 AND brandstoryid=%@",[item objectForKey:@"id"]];
        id video = [[TMSQLite shareInstance] query:sql];
        [item setValue:video forKey:@"video"];
    }
    return items;
}
+(id)brandVideo:(NSString*)video{
    if (video && [video isKindOfClass:NSString.class]) {
        NSString *file = [NSString libraryAppend:video];
        if ([[NSFileManager defaultManager] fileExistsAtPath:file]) {
            return [NSURL fileURLWithPath:file];
        }
        NSString *path = [AppDataRoot stringByAppendingPathComponent:video];
        NSString *app = [path stringByReplacingOccurrencesOfString:@":/" withString:@"://"];
        return [NSURL URLWithString:app];
    }
    return nil;
}
@end